package com.ohuang.test_compose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshots.Snapshot


class TestActivity : AppCompatActivity() {

    val state= mutableStateOf("sss")
    val TAG="TestActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        Snapshot.registerApplyObserver { anies: Set<Any>, snapshot: Snapshot ->
            Log.d(TAG, "registerApplyObserver: anies=$anies ,snapshot=$snapshot")
        }
        Snapshot.registerGlobalWriteObserver {
            Log.d(TAG, "registerGlobalWriteObserver: any=${it} ")
            Snapshot.sendApplyNotifications()
        }
        findViewById<View>(R.id.btn_1).setOnClickListener {
            state.value=""+System.currentTimeMillis()
        }
        findViewById<View>(R.id.btn_2).setOnClickListener {
           Thread{
               Log.d(TAG, "Thread value1=${state.value}")
               Thread.sleep(2000)
               Log.d(TAG, "Thread value2=${state.value}")
           }.start()
        }
        findViewById<View>(R.id.btn_3).setOnClickListener {
           Thread{

               Snapshot.withMutableSnapshot {
                   Thread.sleep(2000)
                   Log.d(TAG, "withMutableSnapshot value=${state.value}")
               }


           }.start()
        }
    }
}